package newfish.contacts.core.service.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.transaction.annotation.Transactional;

import newfish.contacts.core.dao.impl.UserDao;
import newfish.contacts.core.entity.impl.Contact;
import newfish.contacts.core.entity.impl.User;
import newfish.contacts.core.service.UserService;

@Transactional(readOnly=true)
public class UserServiceImpl implements UserService {
	
	private UserDao dao;

	@Override
	@Transactional(readOnly=false)
	public boolean create(User user) {
		dao.create(user);
		return true;
	}

	@Override
	@Transactional(readOnly=false)
	public boolean delete(User user) {
		dao.delete(user);
		return true;
	}

	@Override
	public User findById(String id,boolean init) {
		User user = dao.findById(id);
		if(init){
			for(Contact c : user.getContacts())
				Hibernate.initialize(c);
		}
		return user;
	}

	@Override
	public List<User> listAll() {
		List<User> userList = dao.listAll();
		return userList;
	}

	@Override
	@Transactional(readOnly=false)
	public boolean update(User user) {
		dao.update(user);
		return true;
	}

	@Override
	public User findById(String id) {
		return findById(id,false);
	}

	public void setDao(UserDao dao) {
		this.dao = dao;
	}

}
